package org.jeecg.modules.basic.plan.controller;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.classic.pattern.SyslogStartConverter;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.exitstock.entity.LwgyMaterialExitstock;
import org.jeecg.modules.basic.exitstock.service.ILwgyMaterialExitstockService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.goods.entity.ExportGoods;
import org.jeecg.modules.basic.goods.entity.LwgyMaterialGoods;
import org.jeecg.modules.basic.goods.mapper.LwgyMaterialGoodsMapper;
import org.jeecg.modules.basic.goods.service.ILwgyMaterialGoodsService;
import org.jeecg.modules.basic.material.entity.LwgyInStock;
import org.jeecg.modules.basic.material.service.ILwgyInStockService;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanGoods;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplications;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanapplicationMapper;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanGoodsService;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanapplicationService;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanapplicationsService;
import org.jeecg.modules.basic.plan.vo.LwgyScoreSanitationPage;
import org.jeecg.modules.basic.storages.entity.LwgyStoragesNum;
import org.jeecg.modules.basic.storages.service.ILwgyStoragesNumService;
import org.jeecg.modules.hotel.arrangement.util.ExcelImportPlan;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 物料管理退库表
 * @Author: jeecg-boot
 * @Date:   2021-04-06
 * @Version: V1.0
 */
@Api(tags="物料管理退库表")
@RestController
@RequestMapping("/plan/lwgyMaterialPlanapplication")
@Slf4j
public class LwgyMaterialPlanapplicationController extends JeecgController<LwgyMaterialPlanapplication, ILwgyMaterialPlanapplicationService> {
	@Autowired
	private ILwgyMaterialPlanapplicationService lwgyMaterialPlanapplicationService;

	 @Autowired
	 private ILwgyMaterialPlanapplicationsService lwgyMaterialPlanapplicationsService;


	 @Autowired
	 private ILwgyMaterialPlanGoodsService lwgyMaterialPlanGoodsService;


	 @Resource
	 private LwgyMaterialPlanapplicationMapper lwgyMaterialPlanapplicationMapper;

	 @Autowired
	 private ILwgyMaterialGoodsService lwgyMaterialGoodsService;

	 @Resource
	 private LwgyMaterialGoodsMapper lwgyMaterialGoodsMapper;

	 @Autowired
	 private ILwgyInStockService lwgyInStockService;

	 @Autowired
	 private ILwgyStoragesNumService lwgyStoragesNumService;

	 @Autowired
	 private ISysBaseAPI sysBaseAPI;




	 /**
	  * 自定义分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "lwgy_material_planapplication-自定义分页列表查询")
	 @ApiOperation(value = "lwgy_material_planapplication-自定义分页列表查询", notes = "lwgy_material_planapplication-自定义分页列表查询")
	 @GetMapping(value = "/list1")
	 public Result<?> queryCustomerList(LwgyMaterialPlanapplication lwgyMaterialPlanapplication,
										@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
										@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
										@RequestParam(name = "itemName", required = false) String itemName,
										@RequestParam(name = "writerId", required = false) String writerId,
										@RequestParam(name = "studentCampus", required = false) String studentCampus,
										@RequestParam(name = "state", required = false) String state,
										@RequestParam(name = "category", required = false) String category,
										@RequestParam(name = "reviewState", required = false) String reviewState,
										@RequestParam(name = "progress", required = false) String progress,
										HttpServletRequest req) {

/*
		 QueryWrapper<LwgyMaterialPlanapplication> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialPlanapplication, req.getParameterMap());
*/
		 QueryWrapper<LwgyMaterialPlanapplication> queryWrapper = new QueryWrapper<>();

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		 if (!roles.contains("admin")) {
			 queryWrapper.eq(" writer_id",sysUser.getUsername());
		 }
		 if (StringUtils.isNotBlank(writerId)) {
			 queryWrapper.eq("writer_id",writerId);
		 }
		if (StringUtils.isNotBlank(studentCampus)){
			 queryWrapper.eq("student_campus",studentCampus);
		 }
		 if (StringUtils.isNotBlank(state)){
			 queryWrapper.eq("state",state);
		 }
		 if (StringUtils.isNotBlank(reviewState)){
			 queryWrapper.eq("review_state",reviewState);
		 }
		 if (StringUtils.isNotBlank(progress)){
			 queryWrapper.eq("progress",progress);
		 }
		Page<LwgyMaterialPlanapplication> page = new Page<>(pageNo, pageSize);
		 IPage<LwgyMaterialPlanapplication> pageList = lwgyMaterialPlanapplicationService.queryPageCustomer(page, queryWrapper,itemName,category);
		 return Result.OK(pageList);
	 }


	 @AutoLog(value = "lwgy_material_planapplication-自定义分页列表查询")
	 @ApiOperation(value = "lwgy_material_planapplication-自定义分页列表查询", notes = "lwgy_material_planapplication-自定义分页列表查询")
	 @GetMapping(value = "/list2")
	 public Result<?> queryCustomersList(LwgyMaterialPlanapplications lwgyMaterialPlanapplications,
										@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
										@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
										 @RequestParam(name="id",required=true) String id,
										HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialPlanapplications> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialPlanapplications, req.getParameterMap());
		 Page<LwgyMaterialPlanapplications> page = new Page<>(pageNo, pageSize);
		 IPage<LwgyMaterialPlanapplications> pageList = lwgyMaterialPlanapplicationsService.queryPageCustomer(page, queryWrapper,id);
		 return Result.OK(pageList);
	 }

	 @AutoLog(value = "lwgy_material_planapplication-自定义分页列表查询")
	 @ApiOperation(value = "lwgy_material_planapplication-自定义分页列表查询", notes = "lwgy_material_planapplication-自定义分页列表查询")
	 @GetMapping(value = "/list3")
	 public Result<?> queryCustomerRegisterList(LwgyMaterialPlanapplications lwgyMaterialPlanapplications,
										 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
										 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
										 HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialPlanapplications> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialPlanapplications, req.getParameterMap());
		 Page<LwgyMaterialPlanapplications> page = new Page<>(pageNo, pageSize);
		 IPage<LwgyMaterialPlanapplications> pageList = lwgyMaterialPlanapplicationsService.queryPageRegisterCustomer(page,queryWrapper);
		 return Result.OK(pageList);
	 }


	
	/**
	 * 分页列表查询
	 *
	 * @param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-分页列表查询")
	@ApiOperation(value="物料管理退库表-分页列表查询", notes="物料管理退库表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyMaterialPlanapplication lwgyMaterialPlanapplication,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyMaterialPlanapplication> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialPlanapplication, req.getParameterMap());
		Page<LwgyMaterialPlanapplication> page = new Page<LwgyMaterialPlanapplication>(pageNo, pageSize);
		IPage<LwgyMaterialPlanapplication> pageList = lwgyMaterialPlanapplicationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param
	 * @return
	 *//*
	@AutoLog(value = "物料管理退库表-添加")
	@ApiOperation(value="物料管理退库表-添加", notes="物料管理退库表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		String stockno="";
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss-SSS");
		stockno = "PP" + simpleDateFormat.format(new Date());

		lwgyMaterialPlanapplication.setStockNo(stockno);
		lwgyMaterialPlanapplication.setCategory(lwgyMaterialPlanapplication.getCategory());
		lwgyMaterialPlanapplication.setWriterId(sysUser.getUsername());
		lwgyMaterialPlanapplication.setState("0");
		lwgyMaterialPlanapplication.setReviewState("0");
		lwgyMaterialPlanapplication.setWriteTime(new Date());
		lwgyMaterialPlanapplication.setProgress("0%");
		lwgyMaterialPlanapplication.setInStockprogress("0%");
		if(lwgyMaterialPlanapplication.getPlanNum()<=0||lwgyMaterialPlanapplication.getPlanNum()==null){
			return  Result.error("申请数量不能为0或不能为空!!!!");
		}
		lwgyMaterialPlanapplicationService.save(lwgyMaterialPlanapplication);
		if (lwgyMaterialPlanapplication != null &&lwgyMaterialPlanapplication.getId() != null) {
			LwgyMaterialPlanGoods lwgyMaterialPlanGoods=new LwgyMaterialPlanGoods();
			 lwgyMaterialPlanGoods.setPlanId(lwgyMaterialPlanapplication.getId());
			    lwgyMaterialPlanGoods.setGoodsId(lwgyMaterialPlanapplication.getGoodsId());
			    lwgyMaterialPlanGoods.setBuyNum(lwgyMaterialPlanapplication.getBuyNum());
				lwgyMaterialPlanGoods.setPlanNum(lwgyMaterialPlanapplication.getPlanNum());
				lwgyMaterialPlanGoods.setMark("0");
				lwgyMaterialPlanGoods.setInStockstate("0");
				lwgyMaterialPlanGoodsService.save(lwgyMaterialPlanGoods);
		}
		return Result.OK("添加成功！");
	}




	
	*//**
	 *  编辑
	 *
	 * @param
	 * @return
	 *//*
	@AutoLog(value = "物料管理退库表-编辑")
	@ApiOperation(value="物料管理退库表-编辑", notes="物料管理退库表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		return Result.OK("编辑成功!");
	}
	
	*//**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 *//*
	@AutoLog(value = "物料管理退库表-通过id删除")
	@ApiOperation(value="物料管理退库表-通过id删除", notes="物料管理退库表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyMaterialPlanapplicationService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	*//**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 *//*
	@AutoLog(value = "物料管理退库表-批量删除")
	@ApiOperation(value="物料管理退库表-批量删除", notes="物料管理退库表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyMaterialPlanapplicationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	*//**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 *//*
	@AutoLog(value = "物料管理退库表-通过id查询")
	@ApiOperation(value="物料管理退库表-通过id查询", notes="物料管理退库表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyMaterialPlanapplication lwgyMaterialPlanapplication = lwgyMaterialPlanapplicationService.getById(id);
		if(lwgyMaterialPlanapplication==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyMaterialPlanapplication);
	}*/



	 /**
	  *  编辑
	  *
	  * @param lwgyScoreSanitationPage
	  * @return
	  */
	 @AutoLog(value = "物料管理-编辑")
	 @ApiOperation(value="物料管理-编辑", notes="物料管理-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage) {
		 LwgyMaterialPlanapplication lwgyScoreSanitation = new  LwgyMaterialPlanapplication();
		 BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyScoreSanitation);
		 LwgyMaterialPlanapplication lwgyScoreSanitationEntity =  lwgyMaterialPlanapplicationService
				 .getById(lwgyScoreSanitation.getId());
		 if(lwgyScoreSanitationEntity==null) {
			 return Result.error("未找到对应数据");
		 }
		 lwgyMaterialPlanapplicationService.updateMain(lwgyScoreSanitation, lwgyScoreSanitationPage.getLwgyMaterialPlanGoodsList());
		 return Result.OK("编辑成功!");
	 }



	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "物料管理-通过id删除")
	 @ApiOperation(value="物料管理-物料管理删除", notes="物料管理-通过id删除")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		 lwgyMaterialPlanapplicationService.delMain(id);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "物料管理-批量删除")
	 @ApiOperation(value="卫物料管理-批量删除", notes="物料管理-批量删除")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		 this.lwgyMaterialPlanapplicationService.delBatchMain(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功！");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "物料管理-通过id查询")
	 @ApiOperation(value="物料管理-通过id查询", notes="物料管理-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		 LwgyMaterialPlanapplication lwgyScoreSanitation = lwgyMaterialPlanapplicationService.getById(id);
		 if(lwgyScoreSanitation==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(lwgyScoreSanitation);

	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "物料管理通过主表ID查询")
	 @ApiOperation(value="物料管理主表ID查询", notes="物料管理-通主表ID查询")
	 @GetMapping(value = "/queryLwgyTargetTypeByMainId")
	 public Result<?> queryLwgyTargetTypeListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<LwgyMaterialPlanGoods> lwgyMaterialPlanGoodsList = lwgyMaterialPlanGoodsService.selectByMainId(id);
		 return Result.OK(lwgyMaterialPlanGoodsList);
	 }


	 /**
    * 导出excel
    *
    * @param
    * @param
    */
 /*   @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyMaterialPlanapplication lwgyMaterialPlanapplication ) {
        return super.exportXls(request, lwgyMaterialPlanapplication , LwgyMaterialPlanapplication.class, "物料采购计划管理");
    }*/

	/* *//**
	  * 导出excel
	  *
	  * @param request
	  * @param
	  */
	 @RequestMapping(value = "/importExcelUrlToPlan")
	 public void importExcelUrlToPlan(HttpServletRequest request, HttpServletResponse response, LwgyMaterialPlanapplications lwgyMaterialPlanapplications) throws IOException {
		//获取信息。
		 List<LwgyMaterialPlanapplications> lwgyMaterialPlanapplication = lwgyMaterialPlanapplicationService.queryPageExportListCustom(lwgyMaterialPlanapplications);
		 System.out.println("677898989989:"+lwgyMaterialPlanapplication);
		 IntSummaryStatistics resultNum = lwgyMaterialPlanapplication.stream().mapToInt((item) -> Integer.parseInt(item.getStudentCampus())).summaryStatistics();
		 Integer info = resultNum.getMax();
		 LinkedHashMap<String, List<LwgyMaterialPlanapplications>> lwgyBasePlan = lwgyMaterialPlanapplication.stream()
				 .collect(Collectors.groupingBy(LwgyMaterialPlanapplications::getId,LinkedHashMap::new, Collectors.toList()));
		 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

		 lwgyBasePlan.forEach((key, value) -> {
			 Map<String, Object> map1 = new HashMap<>();
			 List<LwgyMaterialPlanapplications> list = value;

			 list.stream().forEach(p -> {
				 map1.put("studentCampuss", p.getStudentCampuss());
				 map1.put("name", p.getName());
				 map1.put("writerId", p.getWriterId());
				 map1.put("stockNo", p.getStockNo());
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 String formattedDate = sdf.format(p.getWriteTime());
				/* map1.put("writerTime", p.getWriteTime());*/
				 map1.put("writerTime", formattedDate);
				 map1.put("itemName",p.getItemName());
				 map1.put("measurement", p.getMeasurement());
				 map1.put("price", p.getPrice());
				 map1.put("specs", p.getSpecs());
				 map1.put("playNum",p.getPlanNum());
				 map1.put("buyNum",p.getPlanNum());
				 map1.put("note",p.getNote());

				 for (int i = 1; i <= info; i++) {
					 if (p.getStudentCampus().contains(i + "")) {
						 map1.put(i + "itemName", p.getItemName());
						 map1.put(i + "measurement", p.getMeasurement());
						 map1.put(i + "price", p.getPrice());
						 map1.put(i + "specs", p.getSpecs());
						 map1.put(i + "playNum", p.getPlanNum());
						 map1.put(i + "buyNum", p.getBuyNum());
						 map1.put(i + "note", p.getNote());
					 }
				 }
			 });
			 dataList.add(map1);
		 });
		 //获取最大category
		 int weeksize = info;
		 List<ExcelExportEntity> entityList = ExcelImportPlan.ExcelImportUtil(weeksize);
		 Workbook wb = ExcelExportUtil.exportExcel(new ExportParams(null, "sheetName"), entityList, dataList);
		 try {
			 String fileName = "用户信息表";
			 OutputStream output = response.getOutputStream();
			 response.setContentType("application/vnd.ms-excel;charset=utf-8");
			 response.setCharacterEncoding("utf-8");
			 response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "utf-8") + ".xls");
			 wb.write(output);
			 output.close();
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyMaterialPlanapplication.class);
    }
	 /**
	  * 已通过状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审核状态")
	 @ApiOperation(value = "审核", notes = "审核")
	 @PutMapping(value = "/changeStateUrl")
	 public Result<?>changeState(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();//登录人
		 lwgyMaterialPlanapplication.getId();
		 lwgyMaterialPlanapplication.setState("1");//审核通过
		 lwgyMaterialPlanapplication.setCheckNote(lwgyMaterialPlanapplication.getCheckNote());
		 lwgyMaterialPlanapplication.setCheckId(sysUser.getUsername());//审核人
		 lwgyMaterialPlanapplication.setCheckTime(new Date());//审核时间
		 lwgyMaterialPlanapplication.setReviewState("0");//审批状态 0未审批
		 lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 未通过状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审核状态")
	 @ApiOperation(value = "审核", notes = "审核")
	 @PutMapping(value = "/changeNoStateUrl")
	 public Result<?>changeNoState(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();//登录人
		 lwgyMaterialPlanapplication.getId();
		 lwgyMaterialPlanapplication.setState("2");//审核未通过
		 lwgyMaterialPlanapplication.setCheckId(sysUser.getUsername());//审核人
		 lwgyMaterialPlanapplication.setCheckTime(new Date());//审核时间
		 lwgyMaterialPlanapplication.setReviewState("0");//审批状态 0未审批
		 lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 已通过审批状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审批状态")
	 @ApiOperation(value = "审批", notes = "审批")
	 @PutMapping(value = "/changeReviewStateUrl")
	 public Result<?>changeReviewState(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();//登录人
		 lwgyMaterialPlanapplication.getId();
		 lwgyMaterialPlanapplication.setReviewState("1");//审核通过
		 lwgyMaterialPlanapplication.setReviewNote(lwgyMaterialPlanapplication.getReviewNote());
		 lwgyMaterialPlanapplication.setReviewId(sysUser.getUsername());//审批人
		 lwgyMaterialPlanapplication.setReviewTime(new Date());//审核时间
		 lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 未通过审批
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审批状态")
	 @ApiOperation(value = "审批", notes = "审批")
	 @PutMapping(value = "/changeNoReviewStateUrl")
	 public Result<?>changeNoReviewState(@RequestBody LwgyMaterialPlanapplication lwgyMaterialPlanapplication) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();//登录人
		 lwgyMaterialPlanapplication.getId();
		 lwgyMaterialPlanapplication.setReviewState("2");//审核未通过
		 lwgyMaterialPlanapplication.setReviewId(sysUser.getUsername());//审批人
		 lwgyMaterialPlanapplication.setReviewTime(new Date());//审核时间
		 lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		 return Result.OK("保存成功!");
	 }
	 /**
	  *   添加
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "物料管理-添加")
	 @ApiOperation(value="物料管理-添加", notes="物料管理-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add( @RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage) {
		 String stockno="";
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				 "yyyyMMddHHmmss-SSS");
		 stockno = "PP" + simpleDateFormat.format(new Date());
		 LwgyMaterialPlanapplication lwgyMaterialPlanapplication = new LwgyMaterialPlanapplication();
		 BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyMaterialPlanapplication);
		 lwgyMaterialPlanapplication.setStockNo(stockno);
		 lwgyMaterialPlanapplication.setWriterId(sysUser.getUsername());
		 lwgyMaterialPlanapplication.setState("0");
		 lwgyMaterialPlanapplication.setReviewState("0");
		 lwgyMaterialPlanapplication.setWriteTime(new Date());
		 lwgyMaterialPlanapplication.setProgress((double) 0);
		 lwgyMaterialPlanapplication.setInStockProgress((double) 0);
		 lwgyMaterialPlanapplicationService.saveMain(lwgyMaterialPlanapplication,lwgyScoreSanitationPage.getLwgyMaterialPlanGoodsList());
		 if (lwgyMaterialPlanapplication != null &&lwgyMaterialPlanapplication.getId() != null) {
			 String goodsId = lwgyScoreSanitationPage.getGoodsId();
			 String planNum=lwgyScoreSanitationPage.getPlanNum();
			 String [] str = goodsId.split(",");
			 String [] str1=planNum.split(",");
			 List<String> list = Arrays.asList(str);
			 List<String> list1 = Arrays.asList(str1);
			 if (null!=list&&list.size()>0){
				 for (int i =0;i< list.size();i++){
					 LwgyMaterialPlanGoods lwgyMaterialPlanGoods=new LwgyMaterialPlanGoods();
					 lwgyMaterialPlanGoods.setPlanId(lwgyMaterialPlanapplication.getId());
					 lwgyMaterialPlanGoods.setGoodsId(list.get(i));
					 lwgyMaterialPlanGoods.setPlanNum( list1.get(i));
					 lwgyMaterialPlanGoods.setMark("0");
					 lwgyMaterialPlanGoods.setInStockState("0");
					 lwgyMaterialPlanGoodsService.save(lwgyMaterialPlanGoods);
				 }
			 }

		 }

		 return Result.OK("添加成功！");
	 }


	 @AutoLog(value = "确认")
	 @ApiOperation(value = "确认", notes = "确认")
	 @PostMapping(value = "/changeSureStateUrl")
	 public Result<?>changeSureState(@RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage) {
		 LwgyMaterialPlanapplication lwgyMaterialPlanapplication = new  LwgyMaterialPlanapplication();
		 BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyMaterialPlanapplication);
		 LwgyMaterialPlanapplication lwgyScoreSanitationEntity = lwgyMaterialPlanapplicationService
				 .getById(lwgyMaterialPlanapplication.getId());
		 String planId=lwgyScoreSanitationPage.getPlanId();
		 String id=lwgyScoreSanitationPage.getId();
		 String goodsId = lwgyScoreSanitationPage.getGoodsId();
		 String planNum=lwgyScoreSanitationPage.getPlanNum();
		 String buyNum=lwgyScoreSanitationPage.getBuyNum();
		 String providerName=lwgyScoreSanitationPage.getProviderName();
		 String [] strs = id.split(",");
		 String [] str = goodsId.split(",");
		 String [] str1=planNum.split(",");
		 String []str3=planId.split(",");
		 String []str4=buyNum.split(",");
		 String[]str5=providerName.split(",");
		 List<String>lists=Arrays.asList(strs);
		 List<String> list = Arrays.asList(str);
		 List<String> list1 = Arrays.asList(str1);
		 List<String> list3 = Arrays.asList(str3);
		 List<String> list4 = Arrays.asList(str4);
		 List<String> list5 = Arrays.asList(str5);
		 for (int i =0;i< lists.size();i++){
			 LwgyMaterialPlanGoods lwgyMaterialPlanGoods=new LwgyMaterialPlanGoods();
			 lwgyMaterialPlanGoods.setId(lists.get(i));
			 lwgyMaterialPlanGoods.setGoodsId(list.get(i));
			 lwgyMaterialPlanGoods.setBuyNum(list4.get(i));
			 lwgyMaterialPlanGoods.setPlanNum(list1.get(i));
			 lwgyMaterialPlanGoods.setPlanId(list3.get(i));
			 lwgyMaterialPlanGoods.setProviderName(list5.get(i));
			 lwgyMaterialPlanGoods.setMark("1");
			 lwgyMaterialPlanGoods.setInStockState("0");
			 lwgyMaterialPlanGoodsService.updateById(lwgyMaterialPlanGoods);
		 }
		List<Map<String,Object>>list10=lwgyMaterialPlanGoodsService.queryPageList(list3.get(0));
		 Map<String,Object>map = list10.get(0);
		 double a = Double.valueOf(map.get("progress").toString());
		 lwgyMaterialPlanapplication.setProgress(a);
		 lwgyMaterialPlanapplicationsService.changeState(list3.get(0),a);
		 return Result.OK("登记成功!");
		 }





	@AutoLog(value = "物料入库表-入库")
	 @ApiOperation(value="物料入库表-入库", notes="物料入库表-入库")
	 @PostMapping(value = "/changeInState")
	 public Result<?> changeInState(@RequestBody LwgyScoreSanitationPage lwgyScoreSanitationPage,LwgyInStock lwgyInStock,LwgyMaterialGoods lwgyMaterialGoods) {
		 LwgyMaterialPlanapplication lwgyMaterialPlanapplication = new LwgyMaterialPlanapplication();
		 BeanUtils.copyProperties(lwgyScoreSanitationPage, lwgyMaterialPlanapplication);
		String planId=lwgyScoreSanitationPage.getPlanId();
		String id=lwgyScoreSanitationPage.getId();
		String goodsId = lwgyScoreSanitationPage.getGoodsId();
		String buyNum=lwgyScoreSanitationPage.getBuyNum();
		String providerName=lwgyScoreSanitationPage.getProviderName();
		String [] strs = id.split(",");
		String [] str = goodsId.split(",");
		String []str3=planId.split(",");
		String []str4=buyNum.split(",");
		String[]str5=providerName.split(",");
		List<String>lists=Arrays.asList(strs);
		List<String> list = Arrays.asList(str);
		List<String> list3 = Arrays.asList(str3);
		List<String> list4 = Arrays.asList(str4);
		List<String> list5 = Arrays.asList(str5);
		for (int i =0;i< lists.size();i++){
			LwgyMaterialPlanGoods lwgyMaterialPlanGoods=new LwgyMaterialPlanGoods();
			lwgyMaterialPlanGoods.setId(lists.get(i));
			lwgyMaterialPlanGoods.setGoodsId(list.get(i));
			lwgyMaterialPlanGoods.setBuyNum(list4.get(i));
			lwgyMaterialPlanGoods.setPlanId(list3.get(i));
			lwgyMaterialPlanGoods.setProviderName(list5.get(i));
			lwgyMaterialPlanGoods.setInStockState("1");
			lwgyMaterialPlanGoodsService.updateById(lwgyMaterialPlanGoods);
		}
		List<Map<String,Object>>listIn=lwgyMaterialPlanGoodsService.queryPageListIn(list3.get(0));
		if(listIn.size()>0&&listIn!=null){
			Map<String,Object>map = listIn.get(0);
			double a = Double.valueOf(map.get("inStockProgress").toString());
			lwgyMaterialPlanapplication.setInStockProgress(a);
			lwgyMaterialPlanapplicationsService.changeInState(list3.get(0),a);
		}
		 BigDecimal price=lwgyScoreSanitationPage.getPrice();
		 BigDecimal num=new BigDecimal(lwgyScoreSanitationPage.getBuyNum());
		 //2022
		 BigDecimal amout=price.multiply(num);//乘法运算
		 lwgyMaterialPlanapplicationService.updateById(lwgyMaterialPlanapplication);
		 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				 "yyyyMMddHHmmss-SSS");
		 lwgyInStock.setStockNo("R"+ simpleDateFormat.format(new Date()));
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 lwgyInStock.setStudentCampus(lwgyMaterialPlanapplication.getStudentCampus());
		 lwgyInStock.setInStockName(lwgyMaterialPlanapplication.getName());
		 lwgyInStock.setPrice(lwgyScoreSanitationPage.getPrice());
		 lwgyInStock.setInStockCounts(new BigDecimal(lwgyScoreSanitationPage.getBuyNum()));
		 lwgyInStock.setItemName(lwgyScoreSanitationPage.getItemName());
		 //2022
		 lwgyInStock.setAmount(amout);
		 lwgyInStock.setState("1");
		 lwgyInStock.setIsPrint("0");
		 lwgyInStock.setBuyer(sysUser.getUsername());
		 lwgyInStock.setGoodsId(lwgyScoreSanitationPage.getGoodsId());
		 lwgyInStock.setWriter(sysUser.getUsername());
		 lwgyInStock.setWriterTime(new Date());
		 lwgyInStock.setInStockTime(new Date());
		 lwgyInStock.setProviderName(lwgyScoreSanitationPage.getProviderName());
		 lwgyInStock.setInStockMethod(lwgyScoreSanitationPage.getInStockMethod());
		 lwgyInStock.setCheckUser(sysUser.getUsername());
		 lwgyInStock.setCheckDatetime(new Date());
		 lwgyInStockService.save(lwgyInStock);

		 LwgyStoragesNum lwgyStoragesNum=new LwgyStoragesNum();
		 List<LwgyStoragesNum> list10=lwgyStoragesNumService.queryPageList(lwgyInStock.getGoodsId(),lwgyInStock.getInStockName());
		 System.out.println("list10:"+list10);
		 if(list10.size()>=1){
			 lwgyStoragesNum = list10.get(0);
			 //2022
			BigDecimal a=lwgyInStock.getInStockCounts();

			 BigDecimal b=lwgyStoragesNum.getNum();
			 //2022
			 BigDecimal c=a.add(b);
			 //2022
			 lwgyStoragesNum.setNum(c);
			 BigDecimal bD1 = lwgyInStock.getPrice();
			 BigDecimal bD2 = lwgyStoragesNum.getPrice();
			 //2022
			BigDecimal b1 = bD1.multiply(a);
			 BigDecimal b2 = bD2.multiply(b);
			 //2022
			BigDecimal b3 = b1.add(b2);
			 //2022
			 BigDecimal b4 = c;
			 //2022
			BigDecimal bg = b3.divide(b4, 2, BigDecimal.ROUND_HALF_UP);
			 //2022
			 lwgyStoragesNum.setPrice(bg);

		 }else{
		 	System.out.println("走这里了！！！");
			 lwgyStoragesNum.setCkId(lwgyInStock.getInStockName());
			 lwgyStoragesNum.setItemName(lwgyInStock.getItemName());
			 lwgyStoragesNum.setStudentCampus(lwgyInStock.getStudentCampus());
			 lwgyStoragesNum.setGoodsId(lwgyInStock.getGoodsId());
			 lwgyStoragesNum.setMinCount(new BigDecimal("0.00"));
			 lwgyStoragesNum.setMaxCount(new BigDecimal("300.00"));// 库存的最大值为300
			 lwgyStoragesNum.setPrice(lwgyInStock.getPrice());
			 lwgyStoragesNum.setSpecs(lwgyScoreSanitationPage.getSpecs());
			 lwgyStoragesNum.setBrand(lwgyScoreSanitationPage.getBrand());
			 lwgyStoragesNum.setMeasurement(lwgyScoreSanitationPage.getMeasurement());
			 BigDecimal a =lwgyInStock.getInStockCounts();
			 lwgyStoragesNum.setNum(a);
			 lwgyStoragesNumService.save(lwgyStoragesNum);
		 }
		 lwgyStoragesNumService.updateById(lwgyStoragesNum);
		 return Result.OK("添加成功！");
	 }


}
